﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BaoJian.Control
{
    /// <summary>
    /// 地方版的图文混排
    /// </summary>
    public partial class ImgDocList : System.Web.UI.UserControl
    {
        public string Region;
        public bool ShowImage = true;
        public int ImageTitleLength;     
        public int DocTitleLength;
        public int Count;
        public string ColumnID;
        public string ColumnName;

        public int ImageHeight;
        public int ImageWidth;

        private string sql1 = " SELECT TOP 1  sid,oid,pid,left(title,{0}) as Title,summary,ctime,FirstSmallImage  FROM doc WHERE Region='{1}'AND PID='{2}' AND Recommand=1 and publish=1 ORDER BY CTime DESC ";
        private string sql2 = " SELECT TOP {0} sid,oid,pid,left(title,{1}) as Title,summary,ctime FROM doc WHERE Region='{2}'AND PID='{3}' AND Recommand != 1 and publish=1 ORDER BY CTime DESC";
        private DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (!Page.IsPostBack)
            {
                ColumnName = Tools.Common.GetSysObjNameByOID(ColumnID);
                if (ShowImage)
                {
                    string sql = string.Format(sql1,ImageTitleLength, Region, ColumnID) + string.Format(sql2, Count,DocTitleLength, Region, ColumnID);
                    ds = Maticsoft.DBUtility.DbHelperSQL.Query(sql);
                    DataTable dt = ds.Tables[0];
                    //ToDo:以后要去掉这个判断
                    if (dt.Rows.Count > 0)
                    {
                        sb.Append("<div class=\"pictext\"><a href=\"/c.aspx?id=" + dt.Rows[0]["oid"] + "\" target=\"_blank\">");
                        sb.Append("<img class=\"borderedImg\" width=\"" + ImageWidth + "\" height=\"" + ImageHeight + "\" alt=\"" + dt.Rows[0]["title"] + "\" src=\"" + dt.Rows[0]["FirstSmallImage"] + "\" /></a>");
                        sb.Append("<h4><a class=\"title\" href=\"/c.aspx?id=" + dt.Rows[0]["oid"] + "\" target=\"_blank\">" + dt.Rows[0]["title"] + "</a></h4>");
                        sb.Append("<p class=\"summary\"><a href=\"/c.aspx?id=" + dt.Rows[0]["oid"] + "\"></a>" + dt.Rows[0]["summary"] + "</p></div>");
                    }

                }
                else
                {
                    string sql = string.Format(sql1, ImageTitleLength, Region, ColumnID) + string.Format(sql2, Count, DocTitleLength, Region, ColumnID);
                    ds = Maticsoft.DBUtility.DbHelperSQL.Query(sql);
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        sb.Append("<div class=\"pictext\"><h3>");
                        sb.Append("<a class=\"title\" href=\"/c.aspx?id=" + dt.Rows[0]["oid"] + "\" target=\"_blank\">" + dt.Rows[0]["title"] + "</a><h3>");
                        sb.Append("<p class=\"summary\"><a href=\"/c.aspx?id=" + dt.Rows[0]["oid"] + "\"></a>" + dt.Rows[0]["summary"] + "</p></div>");
                    }
                }

                Literal1.Text = sb.ToString();
                Repeater1.DataSource = ds.Tables[1];
                Repeater1.DataBind();
            }
        }
    }
}